
#include <log4cxx/logger.h>

#include "HashHelper.h"

using namespace log4cxx;

static LoggerPtr g_logger = Logger::getLogger("HashHelper");

string get_file_hash_dir(const char * data_root_path, int dir_id) {
	if (NULL == data_root_path || dir_id < 0) {
		LOG4CXX_ERROR(g_logger, __FUNCTION__ << "invalid args");
		return "";
	}
	int h1, h2, t;
	h1 = dir_id % 1024;
	t = dir_id / 1024;
	h2 = t % 1024;
	char p[2048];
	snprintf(p, sizeof(p), "%s/%d/%d", data_root_path, h1, h2);
	return string(p);
}

string get_file_hash_path(const char * data_root_path, int dir_id, int64_t file_id) {
	 if (NULL == data_root_path || dir_id < 0 || file_id < 0) {
                LOG4CXX_ERROR(g_logger, __FUNCTION__ << "invalid args");
                return "";
        }
	string dir = get_file_hash_dir(data_root_path, dir_id);
	char t[2048];
	snprintf(t, sizeof(t), "%s/%lld", dir.c_str(), file_id);
	return string(t);
}
	
string get_control_file_hash_path(const char * data_root_path, int dir_id, int64_t file_id) {
	if (NULL == data_root_path || dir_id < 0 || file_id < 0) {
                LOG4CXX_ERROR(g_logger, __FUNCTION__ << "invalid args");
                return "";
        }
	string s = get_file_hash_path(data_root_path, dir_id, file_id);
	return s+".control";
}
	
